package com.chao.demo.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

@RestController
public class SendEmail {

    @Value(value = "${spring.mail.username}")
    public String SENDER;

    @Resource
    private JavaMailSenderImpl mailSender;

    @ApiOperation(value = "发送邮箱", notes = "发送邮箱")
    @GetMapping("/send")
    public String sendEmail() {
        try {
            //创建一个复杂的消息邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //multipart:true
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("超哥博客");
            helper.setText("这是一个测试是否能发送邮件的文本", true);
            //邮件接收人
            helper.setTo("1660292539@qq.com");
            //邮件发送者
            helper.setFrom("852217515@qq.com");
            mailSender.send(mimeMessage);
            System.out.println("邮件发送成功");
            /*添加邮件附件
            String path = ""; //文件路径
            String fileName = ""; //文件名
            helper.addAttachment(fileName, new File(path));
            */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "邮件发送成功";
    }
}
